const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/demo', {
    useNewUrlParser: true
});

const con = mongoose.connection
con.on('error', error => console.log(error));
con.on('open', () => console.log('connection open'))
con.on('disconnected', () => console.log('disconnected'))


const kittySchema = new mongoose.Schema({
    name: String
})

const Kitty = mongoose.model('Kitty', kittySchema, 'Kitties')

const kit1 = new Kitty({
    name: 'tam'
})
kit1.save(function (err, kit) {
    if (err) throw err
    console.log(kit)
    const kit2 = Kitty.create({
        name: 'mike'
    }, function (err, doc) {
        if (err) throw err
        console.log(doc)
        console.log('Query:')
        Kitty.find(function (err, docs) {
            if (err) throw err
            console.log(docs)
            mongoose.disconnect()
        })
    })
})